#!/bin/bash
TOMCAT_PATH=/home
TOMCAT_PORT=(7001 8001 9001)
#调用函数库
[ -f /etc/init.d/functions ] && source /etc/init.d/functions
export PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
source /etc/profile


[ $(id -u) -gt 0 ] && echo "请用root用户执行此脚本！" && exit 1

if [ $# != 1 ]; then
	echo "Usage: tomcat-install.sh tomcat*.tgz"
	exit 1
fi

tar -zxvf $1 -C ${TOMCAT_PATH}  &>/dev/null && mv ${TOMCAT_PATH}/apache-tomcat-* ${TOMCAT_PATH}/tomcat${TOMCAT_PORT[1]} &>/dev/null

# modify tomcat/conf/context.xml
  sed -i -r -e  '/<\/Context>/i \  <Resource auth="Container" driverClassName="com.mysql.jdbc.Driver" name="jdbc/lcp_db" password="root" type="javax.sql.DataSource" url="jdbc:mysql://xx.xx.xx:3306/lcpdb" username="root"/>' ${TOMCAT_PATH}/tomcat${TOMCAT_PORT[1]}/conf/context.xml

  # modify http_port
  sed -i -r -e 's#.*shutdown="SHUTDOWN">.*#<Server port="'${TOMCAT_PORT[0]}'" shutdown="SHUTDOWN">#' ${TOMCAT_PATH}/tomcat${TOMCAT_PORT[1]}/conf/server.xml
  sed -i -r -e 's#.*protocol="HTTP/1.1".*#<Connector port="'${TOMCAT_PORT[1]}'" protocol="HTTP/1.1"#' ${TOMCAT_PATH}/tomcat${TOMCAT_PORT[1]}/conf/server.xml
  sed -i -r -e 's#.*protocol="org.apache.coyote.http11.Http11NioProtocol".*#<Connector port="'${TOMCAT_PORT[1]}'" protocol="org.apache.coyote.http11.Http11NioProtocol"#' ${TOMCAT_PATH}/tomcat${TOMCAT_PORT[1]}/conf/server.xml
  sed -i -r -e 's#.*protocol="AJP/1.3" redirectPort="8443" />.*#<Connector port="'${TOMCAT_PORT[2]}'" protocol="AJP/1.3" redirectPort="8443" />#' ${TOMCAT_PATH}/tomcat${TOMCAT_PORT[1]}/conf/server.xml
  # startup tomcat
  ${TOMCAT_PATH}/tomcat${TOMCAT_PORT[1]}/bin/startup.sh &>/dev/null

echo -e "\033[33m***************************************************完成tomcat环境部署***************************************************\033[0m"
cat > /tmp/tomcat.log  << EOF
tomcat安装目录：${TOMCAT_PATH}/tomcat${TOMCAT_PORT[1]}
tomcat端口：${TOMCAT_PORT[1]}
EOF
cat /tmp/tomcat.log